import 'package:flutter/material.dart';
import './res/listDate.dart';

//ListView列表组件
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('ListView动态列表 list,maps实现 -- 数据抽离出去'),
          backgroundColor: Colors.pink[50],
        ),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});
  //for实现
  /*
  List<Widget> _initListDate() {
  print('${listDate} + 111');
    List<Widget> list = [];
    for (var i = 0; i < listDate.length; i++) {
      list.add(
        Column(
          children: [
            ListTile(
              title: Text('${listDate[i]['title']}'),
              subtitle: Text('${listDate[i]['date']}'),
              leading: Image.network('${listDate[i]['imageUrl']}'),
            ),
            Divider(),
          ],
        ),
      );
    }
    return list;
  }*/

  //map实现
  List<Widget> initMapDate() {
    var tempList = listDate.map((value) {
      return Column(
        children: [
          ListTile(
            title: Text('${value['title']}'),
            subtitle: Text('${value['date']}'),
            leading: Image.network('${value['imageUrl']}'),
          ),
          Divider(height: 60),
        ],
      );
    });

    return tempList.toList();
  }

  @override
  Widget build(BuildContext context) {
    print(listDate);
    return ListView(
      // children: _initListDate(), //for实现
      children: initMapDate(),//map实现
    );
  }
}
